<template>
  <div class="page" v-loading="loading">
    <div class="gva-card-box">
      <div class="gva-card gva-top-card">
        <div class="gva-top-card-left">
          <div class="gva-top-card-left-title">早安，管理员，请开始一天的工作吧</div>
          <div class="gva-top-card-left-rows">
            <el-row>
              <el-col :span="8" :xs="24" :sm="8">
                <div class="flex-center">
                  <el-icon class="dashboard-icon">
                    <avatar />
                  </el-icon>
                  总用户数 ({{ total.allUser }})
                </div>
              </el-col>
              <el-col :span="8" :xs="24" :sm="8">
                <div class="flex-center">
                  <el-icon class="dashboard-icon">
                    <Stopwatch />
                  </el-icon>
                  盒子启动总次数 ({{ total.boxRunNums }})
                </div>
              </el-col>
              <el-col :span="8" :xs="24" :sm="8">
                <div class="flex-center">
                  <el-icon class="dashboard-icon">
                    <Download />
                  </el-icon>
                  游戏总下载次数 ({{ total.downNums }})
                </div>
              </el-col>
              <el-col :span="8" :xs="24" :sm="8">
                <div class="flex-center">
                  <el-icon class="dashboard-icon">
                    <HelpFilled />
                  </el-icon>
                  游戏总启动次数 ({{ total.startNums }})
                </div>
              </el-col>
              <el-col :span="8" :xs="24" :sm="8">
                <div class="flex-center">
                  <el-icon class="dashboard-icon">
                    <VideoCamera />
                  </el-icon>
                  直播间总点击次数 ({{ total.liveNums }})
                </div>
              </el-col>
            </el-row>
          </div>

        </div>
        <img src="@/assets/dashboard.png" class="gva-top-card-right" alt>
      </div>
    </div>
    <div class="gva-card-box">
      <el-card class="gva-card quick-entrance">
        <template #header>
          <div class="card-header">
            <el-form :inline="true" :model="searchInfo" class="demo-form-inline" @keyup.enter="onSubmit">
              <el-form-item label="时间查询">
                <el-date-picker v-model="searchInfo.startCreatedAt" type="datetime" placeholder="开始时间"></el-date-picker>
                --
                <el-date-picker v-model="searchInfo.endCreatedAt" type="datetime" placeholder="结束时间"></el-date-picker>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
                <el-button icon="refresh" @click="onReset">重置</el-button>
                <el-button type="primary" plain @click="findTime(0)">今天</el-button>
                <el-button type="primary" plain @click="findTime(7)">近7天</el-button>
                <el-button type="primary" plain @click="findTime(31)">近一个月</el-button>

              </el-form-item>
            </el-form>
          </div>
        </template>
        <el-row :gutter="20">
          <el-col v-for="(items, key) in totalList" :key="key" :span="4" :xs="8" class="quick-entrance-items boxIco">
            <div class="quick-entrance-item">
              <p>{{ items.name=items.name.replace(/　/g, ' ') }}</p>
              <p v-for="(item, key) in items.children">
                <span v-if="item.typeDownst == 0">
                  启动次数：{{ item.nums }}
                  <img src="../../assets/01.png" />
                </span>
                <span v-if="item.typeDownst == 1">
                  下载游戏次数：{{ item.nums }}
                  <img src="../../assets/02.png" />
                </span>
                <span v-if="item.typeDownst == 2">
                  进入游戏次数：{{ item.nums }}
                </span>
                <span v-if="item.typeDownst == 3">
                  直播间进入次数：{{ item.nums }}
                  <img src="../../assets/03.png" />
                </span>

              </p>
            </div>
          </el-col>
          <!-- <el-col v-for="(card, key) in toolCards" :key="key" :span="4" :xs="8" class="quick-entrance-items"
            @click="toTarget(card.name)">
            <div class="quick-entrance-item">
              <div class="quick-entrance-item-icon" :style="{ backgroundColor: card.bg }">
                <el-icon>
                  <component :is="card.icon" :style="{ color: card.color }" />
                </el-icon>
              </div>
              <p>{{ card.label }}</p>
            </div>
          </el-col> -->
        </el-row>
      </el-card>
      <!-- <div class="quick-entrance-title"></div> -->
    </div>
    <div class="gva-card-box">
      <div class="gva-card">
        <div class="card-header">
          <span>数据统计</span>
        </div>
        <div class="echart-box">
          <el-row :gutter="20">
            <el-col v-if="totalList.length > 0">
              <echarts-line :totalList=totalList />
            </el-col>
            <!-- <el-col :xs="24" :sm="6">
              <dashboard-table />
            </el-col> -->
          </el-row>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import EchartsLine from '@/view/dashboard/dashboardCharts/echartsLine.vue'
import DashboardTable from '@/view/dashboard/dashboardTable/dashboardTable.vue'
import { formatDate, formatYYYY } from '@/utils/format'
import { getAll, getGameTranslateList } from '@/api/gameTranslate'
import { ref } from 'vue'

const total = ref({});
const totalList = ref([])

const loading = ref(false)

// 查询
const getTableData = async () => {
  loading.value = true
  totalList.value = []
  const res = await getAll()
  total.value = res.data
  let table = await getGameTranslateList({ page: 1, pageSize: 9999, ...searchInfo.value })

  loading.value = false
  table = table.data.list
  let gameName = []
  table.map(item => {
    gameName.push(item.gameName)

  })
  gameName = new Set(gameName)
  gameName = [...gameName]
  let newList = []

  gameName.forEach(element => {
    newList.push({
      name: element,
      children: []
    })
  });


  removeDuplicate(table, 'gameName', 'typeDownst')
  for (let i = 0; i < newList.length; i++) {
    for (let j = 0; j < table.length; j++) {
      if (newList[i].name == table[j].gameName) {
        newList[i].children.push(table[j])
      }
    }

  }
  totalList.value = newList
}

const removeDuplicate = (arr, name1, name2, name3) => {
  for (var i = 0; i < arr.length - 1; i++) {
    for (var j = i + 1; j < arr.length; j++) {
      if (arr[i].gameName === arr[j].gameName && arr[i].typeDownst === arr[j].typeDownst) {
        arr[i].nums = arr[i].nums + arr[j].nums
        arr.splice(j, 1);
        j--;
      }
    }
  }
  return arr
}




const searchInfo = ref({})

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  getTableData()
}

const findTime = (e) => {
  let newT = formatYYYY(new Date())
  newT = formatDate(newT, -24 * e)
  newT = formatYYYY(newT)
  searchInfo.value.startCreatedAt = new Date(newT + " 00:00:00")
  searchInfo.value.endCreatedAt = new Date(formatYYYY(new Date()) + " 23:59:59")
  getTableData()
}
findTime(0)

</script>
<script>
export default {
  name: 'Dashboard'
}
</script>

<style lang="scss" scoped>
@mixin flex-center {
  display: flex;
  align-items: center;
  font-size: 18px;
}

.page {
  background: #f0f2f5;
  padding: 0;

  .gva-card-box {
    padding: 12px 16px;

    &+.gva-card-box {
      padding-top: 0px;
    }
  }

  .gva-card {
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 2px;
    height: auto;
    padding: 26px 30px;
    overflow: hidden;
    box-shadow: 0 0 7px 1px rgba(0, 0, 0, 0.03);
  }

  .gva-top-card {
    height: 260px;
    @include flex-center;
    justify-content: space-between;
    color: #777;

    &-left {
      height: 100%;
      display: flex;
      flex-direction: column;

      &-title {
        font-size: 22px;
        color: #343844;
      }

      &-dot {
        font-size: 16px;
        color: #6B7687;
        margin-top: 24px;
      }

      &-rows {
        // margin-top: 15px;
        margin-top: 18px;
        color: #6B7687;
        width: 800px;
        align-items: center;
      }

      &-item {
        +.gva-top-card-left-item {
          margin-top: 24px;
        }

        margin-top: 14px;
      }
    }

    &-right {
      height: 600px;
      width: 600px;
      margin-top: 28px;
    }
  }

  ::v-deep(.el-card__header) {
    padding: 0;
    border-bottom: none;
  }

  .card-header {
    padding-bottom: 20px;
    border-bottom: 1px solid #e8e8e8;
  }

  .quick-entrance-title {
    height: 30px;
    font-size: 22px;
    color: #333;
    width: 100%;
    border-bottom: 1px solid #eee;
  }

  .quick-entrance-items {
    @include flex-center;
    justify-content: center;
    text-align: center;
    color: #333;

    .quick-entrance-item {
      padding: 16px 28px;
      margin-top: -16px;
      margin-bottom: -16px;
      border-radius: 4px;
      transition: all 0.2s;




      height: auto;
      text-align: center;

      // align-items: center;
      &-icon {
        width: 50px;
        height: 50px !important;
        border-radius: 8px;
        @include flex-center;
        justify-content: center;
        margin: 0 auto;

        i {
          font-size: 24px;
        }
      }

      p {
        margin-top: 10px;
      }
    }
  }

  .echart-box {
    padding: 14px;
  }
}

.boxIco {
  position: relative;
  padding-left: 200px;

  img {
    position: absolute;
    left: 0px;
    top: 10px;
  }
}

.dashboard-icon {
  font-size: 20px;
  color: rgb(85, 160, 248);
  width: 30px;
  height: 30px;
  margin-right: 10px;
  @include flex-center;
}


.flex-center {
  @include flex-center;
}

//小屏幕不显示右侧，将登录框居中
@media (max-width: 750px) {
  .gva-card {
    padding: 20px 10px !important;

    .gva-top-card {
      height: auto;

      &-left {
        &-title {
          font-size: 20px !important;
        }

        &-rows {
          margin-top: 15px;
          align-items: center;
        }
      }

      &-right {
        display: none;
      }
    }

    .gva-middle-card {
      &-item {
        line-height: 20px;
      }
    }

    .dashboard-icon {
      font-size: 18px;
    }
  }
}
</style>
